//
//  TuiHuanViewController.m
//  Experss
//
//  Created by wmwMAC on 17/8/8.
//  Copyright © 2017年 嗯，大葱. All rights reserved.
//

#import "TuiHuanViewController.h"

#import "TuiHuanTableViewCell.h"

#import "TuiHuanDetailViewController.h"

#import "TuiHuanGoodsOneModel.h"
#import "TuiHuanGoodsTwoModel.h"

static NSString *identifier = @"TuiHuanTableViewCell";

@interface TuiHuanViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    BOOL flagAllBtn;//是否全选
}
@property (nonatomic, strong) UITableView *tableView;

//@property (nonatomic, strong)NSMutableArray *btnStateArray;//存放按钮状态
@property (nonatomic, strong)NSMutableArray *dataArray;

@property (nonatomic, strong)TuiHuanGoodsOneModel *tempModel;
@end

@implementation TuiHuanViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    flagAllBtn = NO;
    _dataArray = [NSMutableArray array];
    self.navigationItem.title = @"申请退货";
    
    [self addTableviewUI];
    
    [self addNav];
    [self getTuiHuanListDate];//获取退换货列表
}
-(void)addNav{
    UIBarButtonItem *rightBtnItem = [[UIBarButtonItem alloc] initWithTitle:@"全选" style:(UIBarButtonItemStyleDone) target:self action:@selector(rightItemDone)];
    self.navigationItem.rightBarButtonItem = rightBtnItem;
}

-(void)rightItemDone{
    NSLog(@"诶呀妈呀");
    flagAllBtn = !flagAllBtn;
    if (flagAllBtn == YES) {
        for (TuiHuanGoodsTwoModel *model in self.dataArray) {

            model.selectType = 1;
        }
       
    }
    else {
        for (TuiHuanGoodsTwoModel *model in self.dataArray) {
            model.selectType = 0;

        }
    }
    
    [self.tableView reloadData];
    
}
- (void)addTableviewUI {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight-NAV_HEIGHT) style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.rowHeight = 120;
    self.tableView.backgroundColor = BaseColor;
    [self.tableView registerNib:[UINib nibWithNibName:@"TuiHuanTableViewCell" bundle:nil] forCellReuseIdentifier:identifier];
    [self.view addSubview:self.tableView];
    
    
    UIView *footView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 110)];
    self.tableView.tableFooterView = footView;
    
    UIButton *nextBtn = [BasePublicCreatUI createBtnFrame:CGRectMake(20, 40, kScreenWidth-40, 50) title:@"下一步" bgImage:@"bg1" selectBgImage:nil image:nil target:self action:@selector(nextBtnSelect)];
    nextBtn.layer.masksToBounds = YES;
    nextBtn.layer.cornerRadius = 5;
    [nextBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [footView addSubview:nextBtn];
    
    
}



-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.dataArray.count;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    TuiHuanTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
    
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.NumberBackView.hidden = YES;
 
    [cell.selectBtn addTarget:self action:@selector(handelSeleBtnAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
    TuiHuanGoodsTwoModel *model = self.dataArray[indexPath.row];
    
    if (model.selectType == 0) {
        cell.selectBtn.selected = NO;
    }else{
        cell.selectBtn.selected = YES;
    }
    
    cell.priceLB.text = [NSString stringWithFormat:@"¥%@",model.sku_sell_price_real];
    [cell.imageV sd_setImageWithURL:[NSURL URLWithString:model.sku_image]];
    cell.titleLB.text = model.goods_name;
    //规格
    if (model.sku_value != nil && ![model.sku_value isKindOfClass:[NSNull class]] && model.sku_value.count != 0)
    {
        NSMutableArray *array = [NSMutableArray array];
        for (int i = 0; i < model.sku_value.count; i++) {
            NSString *str1;
            if ([model.sku_value[i][@"type"] intValue] == 2) {
                str1 = model.sku_value[i][@"note"];
            }else {
                str1 = model.sku_value[i][@"value"];
            }
            
            [array addObject:str1];
        }
        
        NSArray *tempAray = [NSArray arrayWithArray:array];
        NSString *string = [tempAray componentsJoinedByString:@" "];
        cell.guigeLB.text = string;

    }else {
        cell.guigeLB.text = @"暂无更多规格";
    }
    
    cell.countLB.text = [NSString stringWithFormat:@"x %@",model.sku_num];
   
    
    
    
    
    
    
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 40;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *headerView = [[UIView alloc] init];
    headerView.backgroundColor = BaseColor;
    
    /*
    UIButton *allBtn = [BasePublicCreatUI createBtnFrame:CGRectMake(10, 10, 20, 20) title:nil bgImage:nil selectBgImage:@"gouwuche_quan2" image:@"gouwuche_quan" target:self action:@selector(allBtnSelect:)];
    allBtn.tag = section+2000;
    [headerView addSubview:allBtn];
     */
    
    UILabel *titleLable = [BasePublicCreatUI createLabelFrame:CGRectMake(10, 0, kScreenWidth-50, 40) title:_shopName textColor:[UIColor blackColor] textAlignment:NSTextAlignmentLeft font:[UIFont systemFontOfSize:15]];
    [headerView addSubview:titleLable];
    
    
    
    return headerView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.001;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *footview = [[UIView alloc] init];
    footview.backgroundColor = BaseColor;
    
    return footview;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    
    TuiHuanGoodsTwoModel *model = self.dataArray[indexPath.row];
    
    if (model.selectType) {
        model.selectType = 0;
    }else{
        model.selectType = 1;
    }
    [self.tableView reloadData];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark 获取退换货列表数据
//http://bs.m0571.com/mobileapi/public/baishi/?service=Shop_OrderList.GetApplyRefund&userid=11&order_id=107
- (void)getTuiHuanListDate {
    
    NSDictionary *saveDic = [SaveDataMethord readLoginData];
    NSDictionary *parameDic = @{@"userid":saveDic[@"userid"],@"order_id":_orderStr,@"actoken":[Md5Apiway getMD5data:saveDic[@"userid"]]};
    [HttpRequest getWithURLString:POST(Shop_OrderListGetApplyRefund) parameters:parameDic success:^(id responseObject) {
     
        if ([responseObject[@"ret"] intValue] != 200) {
            [MBProgressHUD showText:@"申请退换货失败"];
            return ;
        }
        NSString *str = responseObject[@"data"][@"code"];
        if (![[NSString stringWithFormat:@"%@",str] isEqualToString:@"0"]) {
            [MBProgressHUD showText:responseObject[@"data"][@"msg"]];
            return;
        }
        NSDictionary *tuihuaDic = [[responseObject objectForKey:@"data"]objectForKey:@"order_data"];
       
        
        _tempModel = [TuiHuanGoodsOneModel mj_objectWithKeyValues:tuihuaDic];
        
        
        for (TuiHuanGoodsTwoModel *twoModle in _tempModel.sku_list) {
            
            
            [twoModle setSelectType:0];
        }
        

        self.dataArray = [NSMutableArray arrayWithArray:_tempModel.sku_list];


        [self.tableView reloadData];
        
    } failure:^(NSError *error) {
        
    }];
}


- (void)handelSeleBtnAction:(UIButton *)sender {
//    sender.selected = !sender.selected;
    TuiHuanTableViewCell *cell = (TuiHuanTableViewCell *)[[sender superview] superview];
    NSIndexPath *indepath = [self.tableView indexPathForCell:cell];
    
    TuiHuanGoodsTwoModel *model = self.dataArray[indepath.row];
    
    if (model.selectType) {
        model.selectType = 0;
    }else{
        model.selectType = 1;
    }
    [self.tableView reloadData];
}

-(void)nextBtnSelect{
    NSMutableArray *temnpA = [NSMutableArray array];


    for (TuiHuanGoodsTwoModel *modle in self.dataArray) {
        if (modle.selectType == 1) {
            [temnpA addObject:modle.sku_id];
        }
    }
    if (temnpA.count ==0) {
        [MBProgressHUD showText:@"请选择退换货商品"];
        return;
    }
    
    
    NSString *string = [[NSArray arrayWithArray:temnpA] componentsJoinedByString:@","];
    
    
    TuiHuanDetailViewController *vc = [[TuiHuanDetailViewController alloc] init];
    vc.skuIds = string;
    vc.orderID =_tempModel.Id;
    [self.navigationController pushViewController:vc animated:YES];
}
@end
